package io.xccit.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;

/**
 * @author CH_ywx
 * @date 2023-06-22
 * @description swagger配置
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean
    public Docket apiConfig(){
        ArrayList<Parameter> params = new ArrayList<>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.name("token")
                .description("用户token")
                .defaultValue("")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        params.add(parameterBuilder.build());
        Docket apiDocket = new Docket(DocumentationType.SWAGGER_2);
        apiDocket.groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("io.xccit"))
                .paths(PathSelectors.regex("/api/.*"))
                .build()
                .globalOperationParameters(params);
        return apiDocket;
    }

    public ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("后台管理端API")
                .description("管理员的API定义")
                .version("v1.0")
                .contact(new Contact("xccit","http://xccit.io","darkhorse_1209@outlook.com"))
                .build();
    }
}
